package com.ph.demo.current;

/**
 * 必须拿到object监视器才能往下走
 *
 * synchronized块必须执行完毕
 *
 * notify 唤醒等待在当前锁的一个线程
 *
 * notifyAll 唤醒等待在当前锁的所有线程
 */
public class WaitNotifyDemo {


    static Object object = new Object();

    public static  class  T2 extends Thread{


        @Override
        public void run() {
            synchronized (object){

                System.out.println("t2.start");

                object.notify();

                System.out.println("t2.end");

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static class  T1 extends Thread{

        @Override
        public void run() {
            synchronized (object){

                System.out.println("t1.start");

                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("t1.end");

            }
        }
    }

    public static void main(String[] args) throws Exception{
        T2 t2 = new T2();
        T1 t1 = new T1();

        t1.start();
        t2.start();
        t1.join();
        t2.join();



    }



}
